/**
* @description: 979. 在二叉树中分配硬币
* @author hewei
* @date 2022/9/29 15:23
* @version 1.0
*/

public class DistributeCoins {

    public int distributeCoins(TreeNode root) {
        return process(root)[1];
    }

    public int[] process(TreeNode node) {
        if (node == null) return new int[]{0, 0};
        int[] l = process(node.left);
        int[] r = process(node.right);
        return new int[]{node.val + l[0] + r[0] - 1, l[1] + r[1] + Math.abs(l[0]) + Math.abs(r[0])};
    }
}
